<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" size="medium" placeholder="激活码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />

      <el-select v-model="listQuery.type" clearable placeholder="请选择" style="width: 200px;" class="filter-item">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button v-waves class="filter-item" type="primary" size="medium" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" size="medium" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>

      <div class="block">
        <el-table
          :data="tableData"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id">
          <el-table-column
            prop="id"
            label="ID"
            width="80">
          </el-table-column>
          <el-table-column
            prop="code"
            label="激活码">
          </el-table-column>
          <el-table-column
            prop="activate_at"
            label="状态"
            :filters="[{ text: '已激活', value: 1 }, { text: '未激活', value: 0 }]"
            :filter-method="filterTag"
            filter-placement="bottom-end">
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.activate_at > 0 ? 'primary' : 'success'"
                disable-transitions>{{scope.row.activate_at > 0 ? '已激活' : '未激活' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="created_at"
            label="创建时间">
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-popconfirm confirm-button-text='好的' cancel-button-text='不用了' icon="el-icon-info" icon-color="red" title="确定删除吗？" @onConfirm="() => handleDelete(scope.$index, scope.row)">
                                <el-button slot="reference" size="mini" type="danger">删除</el-button>
              </el-popconfirm>

            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" @close="closeDialog">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style=" margin-left:50px;">
        <el-form-item label="生成数量" prop="num">
          <el-input v-model="temp.num" type="number" min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          保存
        </el-button>
      </div>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { abolishActivate, createActivate, getActivateList } from '@/api/activate'
export default {
  name: 'Activate',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        title: undefined
      },
      tableData: [],
      textMap: {
        update: '编辑',
        create: '添加'
      },
      dialogStatus: '',
      dialogFormVisible: false,
      rules: {
        num: [{ required: true, message: '数量不能为空', trigger: 'blur' }]
      },
      temp: {
        num: 0
      },
      options: [{
        value: 0,
        label: '未激活'
      }, {
        value: 1,
        label: '已激活'
      }]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getActivateList(this.listQuery).then(response => {
        if (response.code === 200) {
          this.tableData = response.data.rows
          this.total = response.data.total
        }
        this.resetTemp()
      })
    },
    handleFilter() {
      this.getList()
    },
    resetTemp() {
      this.temp = {
        num: 0
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(index, row) {
      abolishActivate({ ids: [row.id] }).then(response => {
        if (response.code === 200) {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.dialogFormVisible = false
          this.getList()
        }
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.temp)
          createActivate(this.temp).then(response => {
            if (response.code === 200) {
              this.$message({
                message: response.msg,
                type: 'success'
              })
              this.dialogFormVisible = false
              this.getList()
            }
          })
        }
      })
    },
    closeDialog() {
      this.resetTemp()
    },
    filterTag(value, row) {
      if (value > 0){
        return row.activate_at > 0
      }else {
        return row.activate_at === value
      }

    }
  }
}
</script>

<style scoped>

</style>
